In this tangent story we will dive into detailed data on demand! We will look at a series of maps which shows accidents with a given degree of severity and contains the specific circumstances of that accident. This will give you all the information you need to judge what caused the accidents. Without further ado, let us dive into the first map!
from IPython.display import HTML
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
The raw code for this IPython notebook is by default hidden for easier reading.
To toggle on/off the raw code, click <a href="javascript:code_toggle()">here</a>.''')
# Install necessary packages
#!pip install geojson
# Imports
from IPython.display import display
from IPython.core.display import display, HTML
import folium
from folium.plugins import FastMarkerCluster
import tempfile
import plotly.express as px
import geojson
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import plotly.express as px
sns.set_style('darkgrid')
%matplotlib inline
# Load data
df = pd.read_csv('cleaned_data.csv')
df['date'] = pd.to_datetime(df.date)
lat_lon = df[df.severity_name == 'Killed'][['longS','latS','severity','date_full','severity_name','gender_name','age','lum_name',
'atm_name','roadtype','traffic lanes','surface condition','crude_vc','collision_type',
'User category','trip purpose','safety','prof','plan']].sample(10000)
locations = lat_lon[['latS','longS']]
locationlist = locations.values.tolist()
Below a map over fatal accidents can be seen. Remember to click on the points to get characteristics over the accident, victim and surroundings!
# Possible styles for folium, last do not work properly
t_list = ["Stamen Terrain", "Stamen Toner", "Mapbox Bright", 'Cartodb Positron']
# Define the map with coordinates, style and zoom
m = folium.Map(location=[47.092038, 2.392312],
tiles = t_list[3],
zoom_start = 6)
# Define colours
colours = ['lightgreen','black','darkred','orange']
# Unscathed, killed, hospitalized, light injury
# Add markers for each accident
for point in range(0, len(locationlist[:4000])):
html = '''
Accident characteristics:<br>
Date: {}<br>
Severity: {}<br><br>
Victim characteristics:<br>
Age: {}<br>
Gender: {}<br>
Trip purpose: {}<br>
Vehicle type: {}<br>
User status: {}<br>
Collision type: {}<br>
Safety: {}<br><br>
Environment characteristics:<br>
Roadtype: {}<br>
Surface conditions: {}<br>
Lumination: {}<br>
Atmosphere: {}<br>
Vertical profile of road: {}<br>
Horizontal profile of road: {}<br>
Traffic lanes: {}
'''.format(lat_lon.date_full.iloc[point],
lat_lon.severity_name.iloc[point],
lat_lon.age.iloc[point],
lat_lon.gender_name.iloc[point],
lat_lon['trip purpose'].iloc[point],
lat_lon.crude_vc.iloc[point],
lat_lon['User category'].iloc[point],
lat_lon.collision_type.iloc[point],
lat_lon.safety.iloc[point],
lat_lon.roadtype.iloc[point],
lat_lon['surface condition'].iloc[point],
lat_lon.lum_name.iloc[point],
lat_lon.atm_name.iloc[point],
lat_lon.prof.iloc[point],
lat_lon.plan.iloc[point],
lat_lon['traffic lanes'].iloc[point])
#lat_lon.infrastructure.iloc[point])
iframe = folium.IFrame(html,width=400,height=300)
popup = folium.Popup(iframe,max_width=500)
folium.CircleMarker(locationlist[point], radius=2, popup=popup, color=colours[lat_lon['severity'].iloc[point]-1]).add_to(m)
# Add clusters of number of accidents in areas
#m.add_child(FastMarkerCluster(locations[['latS', 'longS']].values.tolist()))
# Display map
m